package com.example.portlocalforward.controller.model;

import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 分页响应模型
 * 
 * @author Generated
 * @version 1.0.0
 */
@ApiModel(description = "分页响应数据")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResponse<T> {
    
    @ApiModelProperty(value = "数据列表")
    private List<T> content;
    
    @ApiModelProperty(value = "当前页码（从0开始）")
    private int page;
    
    @ApiModelProperty(value = "每页大小")
    private int size;
    
    @ApiModelProperty(value = "总元素数量")
    private long totalElements;
    
    @ApiModelProperty(value = "总页数")
    private int totalPages;
    
    @ApiModelProperty(value = "是否为第一页")
    private boolean first;
    
    @ApiModelProperty(value = "是否为最后一页")
    private boolean last;
    
    @ApiModelProperty(value = "当前页元素数量")
    private int numberOfElements;
    
    public static <T> PageResponse<T> of(List<T> content, int page, int size, long totalElements) {
        PageResponse<T> response = new PageResponse<>();
        response.setContent(content);
        response.setPage(page);
        response.setSize(size);
        response.setTotalElements(totalElements);
        response.setTotalPages((int) Math.ceil((double) totalElements / size));
        response.setFirst(page == 0);
        response.setLast(page >= response.getTotalPages() - 1);
        response.setNumberOfElements(content.size());
        return response;
    }
}
